// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    userInfo: {},
    articleNum: 0,
    collectNum: 0,
    zanNum: 0
  },
  // 去用户资料
  goProfile() {
    wx.navigateTo
    wx.navigateTo({
      url: '/pkgUser/pages/profile/profile'
    });
  },
  // 去登录
  goLogin() {
    wx.redirectTo({
      url: '/pkgUser/pages/login/login'
    })
  },

  logout() {
    wx.removeStorageSync('token')
    getApp().token = ''
    wx.redirectTo({
      url: '/pkgUser/pages/login/login'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ isLogin: !!getApp().token })
    if (!this.data.isLogin) return
  },
  async getUserInfo() {
    const { data: { articleNum, collectNum, zanNum, userInfo } } = await wx.http.get('/user/getUserInfo')
    this.setData({ articleNum, collectNum, zanNum, userInfo })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
    // this.getTabBar获取自定义tabBar组件实例，设置对应显示高亮
    this.getTabBar().setData({ selected: 3 })
  },
})